<?php
$error = 0;
$updated = false;

// Actionhandler
if(isset($_POST['save'])){
	if($_POST['save'] == "password_md5"){
		if($_POST['password_md5_1_text']==$_POST['password_md5_2_text']){
			$value = $_POST[ $_POST['save'] . "_2_text" ];
			updateUser($_POST['save'], $value);
			$updated = true;
		}else{
			$error = 1;
		}
	}else if($_POST['save'] == "username"){
		$username = $_POST[ $_POST['save'] . "_text" ];
		if(userExists($username)){
			$error = 2;
		}else{
			updateUser("username", $username);
			$updated = true;
		}
	}else{
		$field_name = $_POST['save'];
		$value = $_POST[ $field_name . "_text" ];
		updateUser($field_name, $value);
		$updated = true;
	}
}

// Title
$output  = "<h1>User settings</h1>";
$output .= "<form action='#' method='post'>";

// Get user information
$username = getUserInformation($_SESSION['user_id'], "username");
$name 	  = getUserInformation($_SESSION['user_id'], "name");
$surname  = getUserInformation($_SESSION['user_id'], "surname");
$street   = getUserInformation($_SESSION['user_id'], "street");
$zip	  = getUserInformation($_SESSION['user_id'], "ZIP");
$country  = getUserInformation($_SESSION['user_id'], "country");
$password = getUserInformation($_SESSION['user_id'], "password_md5");

// Errormessage
if($error == 1){
	$output .= "<font color=red>Password don't match</font>";
	$error = 0;
}else if($error == 2){
	$output .= "<font color=red>Username already exists</font>";
	$error = 0;
}else if($updated){
	$output .= "<font color=green>Updated</font>";
	$updated = 0;
}

// Form
$output .= "<table width=100%>";
	$output .= "<tr>";
		$output .= "<th>Username</th>";
		if(isset($_POST['edit']) && $_POST['edit'] == 1){
			$output .= "<td><input type=text name=username_text value=$username ></td>";
			$output .= "<td><input type='image' title=Save src='img/icon/save.gif' name='save' value=username /></td>";
		}else{
			$output .= "<td>$username</td>";
			$output .= "<td><input type='image' title=Edit src='img/icon/edit.gif' name='edit' value=1 /></td>";
		}
	$output .= "<tr>";
	$output .= "<tr>";
		$output .= "<th>Name</th>";
		if(isset($_POST['edit']) && $_POST['edit'] == 2){
			$output .= "<td><input type=text name=name_text value=$name ></td>";
			$output .= "<td><input type='image' title=Save src='img/icon/save.gif' name='save' value=name /></td>";
		}else{
			$output .= "<td>$name</td>";
			$output .= "<td><input type='image' title=Edit src='img/icon/edit.gif' name='edit' value=2 /></td>";
		}
	$output .= "<tr>";
	$output .= "<tr>";
		$output .= "<th>Surname</th>";
		if(isset($_POST['edit']) && $_POST['edit'] == 3){
			$output .= "<td><input type=text name=surname_text value=$surname ></td>";
			$output .= "<td><input type='image' title=Save src='img/icon/save.gif' name='save' value=surname /></td>";
		}else{
			$output .= "<td>$surname</td>";
			$output .= "<td><input type='image' title=Edit src='img/icon/edit.gif' name='edit' value=3 /></td>";
		}
	$output .= "<tr>";
	$output .= "<tr>";
		$output .= "<th>Street</th>";
		if(isset($_POST['edit']) && $_POST['edit'] == 4){
			$output .= "<td><input type=text name=street_text value=$street ></td>";
			$output .= "<td><input type='image' title=Save src='img/icon/save.gif' name='save' value=street /></td>";
		}else{
			$output .= "<td>$street</td>";
			$output .= "<td><input type='image' title=Edit src='img/icon/edit.gif' name='edit' value=4 /></td>";
		}
	$output .= "<tr>";
	$output .= "<tr>";
		$output .= "<th>ZIP</th>";
		if(isset($_POST['edit']) && $_POST['edit'] == 5){
			$output .= "<td><input type=text name=zip_text value=$zip ></td>";
			$output .= "<td><input type='image' title=Save src='img/icon/save.gif' name='save' value=zip /></td>";
		}else{
			$output .= "<td>$zip</td>";
			$output .= "<td><input type='image' title=Edit src='img/icon/edit.gif' name='edit' value=5 /></td>";
		}
	$output .= "<tr>";
	$output .= "<tr>";
		$output .= "<th>Country</th>";
		if(isset($_POST['edit']) && $_POST['edit'] == 6){
			$output .= "<td><input type=text name=country_text value=$country ></td>";
			$output .= "<td><input type='image' title=Save src='img/icon/save.gif' name='save' value=country /></td>";
		}else{
			$output .= "<td>$country</td>";
			$output .= "<td><input type='image' title=Edit src='img/icon/edit.gif' name='edit' value=6 /></td>";
		}
	$output .= "<tr>";
	$output .= "<tr>";
		$output .= "<th>Password</th>";
		if(isset($_POST['edit']) && $_POST['edit'] == 7){
			$output .= "<td><input type=password name=password_md5_1_text ></td>";
			$output .= "<td></td>";
	$output .= "<tr>";
	$output .= "<tr>";
		$output .= "<th>Repeat</th>";
			$output .= "<td><input type=password name=password_md5_2_text ></td>";
			$output .= "<td><input type='image' title=Save src='img/icon/save.gif' name='save' value=password_md5 /></td>";
	$output .= "<tr>";
		}else{
			$output .= "<td>$password</td>";
			$output .= "<td><input type='image' title=Edit src='img/icon/edit.gif' name='edit' value=7 /></td>";
	$output .= "<tr>";
		}
$output .= "</table>";

$output .= "</form>";

echo $output;

?>